package com.crm.service.impl.returnMoney;

import com.crm.common.model.DhPageInfo;
import com.crm.dao.mapper.customMapper.returnMoneyManagerCustomMapper.FinishOrderProductCustomMapper;
import com.crm.dao.mapper.finishOrderProduct.FinishOrderProductMapper;
import com.crm.dao.mapper.returnMoney.ReturnMoneyFinishOrderProductMapper;
import com.crm.model.entity.finishOrderProduct.FinishOrderProduct;
import com.crm.model.entity.finishOrderProduct.FinishOrderProductExample;
import com.crm.model.entity.finishOrderProduct.PersonalPerformanceAnalysis;
import com.crm.model.vo.customerManager.FinishOrderCustomerVO;
import com.crm.model.vo.customerManager.NewSingleMemberStatisticsVO;
import com.crm.model.vo.finishOrderProduct.FinishOrderProductVO;
import com.crm.model.vo.productManager.AnalyseFinishOrderProductVO;
import com.crm.model.vo.productManager.ProductAnalyseVO;
import com.crm.model.vo.returnMoney.CustFinanceProductOderVO;
import com.crm.model.vo.returnMoney.ProductOfCustomerVO;
import com.crm.service.returnMoney.FinishOrderDBservice;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;


/**
 * @Author: chaifuyou
 * @date 2021/3/26 9:20
 */
@Service
public class FinishOrderDBserviceImpl implements FinishOrderDBservice {

    @Autowired
    private ReturnMoneyFinishOrderProductMapper returnMoneyFinishOrderProductMapper;

    @Autowired
    private FinishOrderProductCustomMapper finishOrderProductCustomMapper;

    @Autowired
    private FinishOrderProductMapper finishOrderProductMapper;

    @Override
    public void addFinishOrderList(List<FinishOrderProduct> finishList) {
        returnMoneyFinishOrderProductMapper.addFinishOrderList(finishList);
    }

    /**
     * 客户详情产品明细
     * @param delState 删除状态  0
     * @param customerId 客户id
     * @param productCategories 产品分类 0:非会员;1:会员
     * @param order 分组类型
     * @return
     */
    @Override
    public List<CustFinanceProductOderVO> finishOrderProductList(short delState, String customerId, String productCategories, String order) {
        return finishOrderProductCustomMapper.finishOrderProductList(delState,customerId,productCategories,order);
    }

    /**
     * 客户详情产品明细
     * @param delState 删除状态  0
     * @param customerId 客户id
     * @param order 分组类型
     * @return
     */
    @Override
    public List<CustFinanceProductOderVO> csdFinishOrderProductList(short delState, String customerId, String order) {
        return finishOrderProductCustomMapper.csdFinishOrderProductList(delState,customerId,order);
    }

    @Override
    public List<CustFinanceProductOderVO> getMemberProductListByOrderId(short delState, String orderId,String productCategories) {
        return finishOrderProductCustomMapper.getMemberProductListByOrderId(delState,orderId,productCategories);
    }

    /**
     * 根据订单id清空产品
     * @param productOrderId
     */
    @Override
    public void delOrderProductByOrderId(String productOrderId) {
        finishOrderProductCustomMapper.delOrderProductByOrderId(productOrderId);
    }

    /**
     * 查询会员产品数量list
     * @param dateUnit
     * @param accountId
     * @return
     */
    @Override
    public List<String> selectVipProductCountList(Integer dateUnit, String accountId,String startDate,String endDate) {
        return finishOrderProductCustomMapper.selectVipProductCountList(dateUnit,accountId,startDate,endDate);
    }

    /**
     * 查询非会员产品数量list
     * @param dateUnit
     * @param accountId
     * @return
     */
    @Override
    public List<String> selectProductCountList(Integer dateUnit, String accountId,String startDate,String endDate) {
        return finishOrderProductCustomMapper.selectProductCountList(dateUnit,accountId,startDate,endDate);
    }

    /**
     * 查询产品额度list
     * @param dateUnit
     * @param accountId
     * @return
     */
    @Override
    public List<String> selectProductPriceCountList(Integer dateUnit, String accountId,String startDate,String endDate) {
        return finishOrderProductCustomMapper.selectProductPriceCountList(dateUnit,accountId,startDate,endDate);
    }

    /**
     * 查询产品总数量、额度
     * @param dateUnit
     * @param accountId
     * @param type
     * @return
     */
    @Override
    public ProductAnalyseVO selectProductCountAndPrice(Integer dateUnit, String accountId, int type,String startDate,String endDate) {
        return finishOrderProductCustomMapper.selectProductCountAndPrice(dateUnit,accountId,type,startDate,endDate);
    }

    /**
     * 查询产品总数量、额度（查询finish表的）
     * @param dateUnit
     * @param accountId
     * @param type
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public ProductAnalyseVO queryFinishProductCountAndPrice(Integer dateUnit, String accountId, int type, String startDate, String endDate) {
        return finishOrderProductCustomMapper.queryFinishProductCountAndPrice(dateUnit,accountId,type,startDate,endDate);
    }

    /**
     * 订单完成的产品列表统计
     * @param dateUnit
     * @param order
     * @param accountId
     * @return
     */
    @Override
    public List<AnalyseFinishOrderProductVO> selectAnalyseFinishOrderProductList(Integer dateUnit, StringBuilder order, String accountId) {
        return finishOrderProductCustomMapper.selectAnalyseFinishOrderProductList(dateUnit,order.toString(),accountId);
    }

    /**
     * 查询用户销售总额度
     * @param dateUnit
     * @param accountId
     * @return
     */
    @Override
    public BigDecimal getUserProductPriceCount(Integer dateUnit, String accountId) {
        return finishOrderProductCustomMapper.getUserProductPriceCount(dateUnit,accountId);
    }

    /**
     * 客户详情产品等级
     * @param delState  删除状态  0
     * @param customerId 客户id
     * @param productCategories 产品分类 0:非会员:1:会员
     * @return
     */
    @Override
    public Integer productMembershipLevel(short delState, String customerId, String productCategories) {
        return finishOrderProductCustomMapper.productMembershipLevel(delState,customerId,productCategories);
    }

    /***
     * @Description:根据产品分类和订单id查询会员产品列表
     * @Param: productCategories 产品分类
     * @Param: orderId 订单id
     * @Author: luojie
     * @Date: 2021/4/16 8:40
     */
    @Override
    public List<FinishOrderProduct> selectFinishOrderProductList(String productCategories, String orderId) {
        FinishOrderProductExample finishOrderProductExample = new FinishOrderProductExample();
        FinishOrderProductExample.Criteria criteria = finishOrderProductExample.createCriteria();
        criteria.andDelFlagEqualTo("0");
        if (Objects.nonNull(productCategories)){
            criteria.andProductCategoriesEqualTo(productCategories);
        }
        criteria.andProductOrderIdEqualTo(orderId);
        List<FinishOrderProduct> finishOrderProductList = finishOrderProductMapper.selectByExample(finishOrderProductExample);
        return finishOrderProductList;
    }

    /***
     * @Description:根据产品分类和订单id和订单明细类型--1:产品类型 查询会员产品列表
     * @Param: productCategories 产品分类
     * @Param: orderId 订单id
     * @Author: baijian
     * @Date: 2021/7/1 15:17
     */
    @Override
    public List<FinishOrderProduct> selectFinishOrderProductList(String orderDetailType,String productCategories, String orderId) {
        FinishOrderProductExample finishOrderProductExample = new FinishOrderProductExample();
        FinishOrderProductExample.Criteria criteria = finishOrderProductExample.createCriteria();
        criteria.andDelFlagEqualTo("0");
        criteria.andOrderDetailTypeEqualTo(orderDetailType);
        if (Objects.nonNull(productCategories)){
            criteria.andProductCategoriesEqualTo(productCategories);
        }
        criteria.andProductOrderIdEqualTo(orderId);
        List<FinishOrderProduct> finishOrderProductList = finishOrderProductMapper.selectByExample(finishOrderProductExample);
        return finishOrderProductList;
    }


    /**
     * @Description: 根据产品编码查询客户信息
     * @Param: [productCode]
     * @Return: java.util.List<com.crm.model.vo.customerManager.FinishOrderCustomerVO>
     * @Author: lixq-fs
     * @Date: 2021/6/2
     **/
    @Override
    public List<FinishOrderCustomerVO> selectCustomersByProductCode(String productCode, String accountId) {
        return finishOrderProductCustomMapper.selectCustomersByProductCode(productCode,accountId);
    }


    /**
     * @Description: 根据客户id获取客户下产品信息
     * @Param: [customerId]
     * @Return: java.util.List<com.crm.model.vo.returnMoney.ProductOfCustomerVO>
     * @Author: lixq-f
     * @Date: 2021/6/3
     **/
    @Override
    public List<ProductOfCustomerVO> selectProductsByCustomerId(String customerId,String productCode) {
        return finishOrderProductCustomMapper.selectProductsByCustomerId(customerId, productCode);
    }

    /**
     * 新增订单完成的产品表
     * @Author pym
     * @Date  2021/6/4 15:16
     * @param finishOrderProduct 订单完成的产品
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean insertFinishOrderProduct(FinishOrderProduct finishOrderProduct) {
        return finishOrderProductMapper.insertSelective(finishOrderProduct) > 0;
    }


    /**
     * @Description: 根据非会员产品编号消费产品
     * @Param: [productNumLst]
     * @Return: void
     * @Author: lixq-f
     * @Date: 2021/6/11
     **/
    @Override
    public Boolean consumeNoMember(List<String> productNumLst) {
        return finishOrderProductCustomMapper.consumeNoMember(productNumLst) > 0;
    }


    @Override
    public List<ProductOfCustomerVO> queryProductsByProductNumLst(List<String> productNumLst) {
        return finishOrderProductCustomMapper.queryProductsByProductNumLst(productNumLst);
    }


    @Override
    public Boolean updateServiceTimesById(String finishOrderProductId, int remainNum) {
        FinishOrderProduct product = new FinishOrderProduct();
        product.setFinishOrderProductId(finishOrderProductId);
        product.setProductStrokesServiceNumber(remainNum);
        return finishOrderProductMapper.updateByPrimaryKeySelective(product) > 0;
    }


    @Override
    public FinishOrderProduct getProductByProductNum(String productNum) {
        FinishOrderProductExample example = new FinishOrderProductExample();
        FinishOrderProductExample.Criteria criteria = example.createCriteria();
        criteria.andFinishOrderProductNumEqualTo(productNum);
        List<FinishOrderProduct> products = finishOrderProductMapper.selectByExample(example);
        return products.get(0);
    }

    /**
     * 通过订单id查询订单完成的产品
     * @param orderId 订单id
     * @return
     */
    @Override
    public List<FinishOrderProduct> finishOrderBackProductList(String orderId) {
        FinishOrderProductExample example = new FinishOrderProductExample();
        FinishOrderProductExample.Criteria criteria = example.createCriteria();
        criteria.andProductOrderIdEqualTo(orderId);
        criteria.andOrderDetailTypeEqualTo("1");
        criteria.andDelFlagEqualTo("0");
        criteria.andProductStatusEqualTo("3");
        List<FinishOrderProduct> list = finishOrderProductMapper.selectByExample(example);
        return list;
    }

    /**
     * 通过产品号查询订单下完成的产品
     * @param finishOrderProductNum 产品号
     * @return
     */
    @Override
    public FinishOrderProduct selectFinshByProductNum(String finishOrderProductNum) {
        FinishOrderProductExample example = new FinishOrderProductExample();
        FinishOrderProductExample.Criteria criteria = example.createCriteria();
        criteria.andFinishOrderProductNumEqualTo(finishOrderProductNum);
        criteria.andDelFlagEqualTo("0");
        List<FinishOrderProduct> list = finishOrderProductMapper.selectByExample(example);
        if(list != null && list.size() > 0){
            return list.get(0);
        }else{
            return null;
        }

    }

    /**
     * 根据订单id查询总业绩金额
     * @param productOrderId
     * @return
     */
    @Override
    public BigDecimal selectGradeMoneyByOrderId(String productOrderId) {
        return finishOrderProductCustomMapper.selectGradeMoneyByOrderId(productOrderId);
    }

    /**
     * 查询业绩金额
     * @param companyId
     * @param dateType
     * @return
     */
    @Override
    public BigDecimal selectGradeMoneyCount(String companyId, Integer dateType) {
        return finishOrderProductCustomMapper.selectGradeMoneyCount(companyId,dateType);
    }

    @Override
    public List<AnalyseFinishOrderProductVO> getPerformanceList(Integer dateUnit, StringBuilder order, String accountId) {
        return finishOrderProductCustomMapper.getPerformanceList(dateUnit,order.toString(),accountId);
    }

    /**
     * 根据订单id查询提成金额
     * @param orderId
     * @return
     */
    @Override
    public BigDecimal selectFinishOrderProductRoyaltyMoneyCountByOrderId(String orderId) {
        return finishOrderProductCustomMapper.selectFinishOrderProductRoyaltyMoneyCountByOrderId(orderId);
    }

    @Override
    public FinishOrderProduct selectFinishOrderProductById(String finishOrderProductId) {
        FinishOrderProductExample finishOrderProductExample = new FinishOrderProductExample();
        FinishOrderProductExample.Criteria criteria = finishOrderProductExample.createCriteria();
        criteria.andFinishOrderProductIdEqualTo(finishOrderProductId);
        List<FinishOrderProduct> finishOrderProductList = finishOrderProductMapper.selectByExample(finishOrderProductExample);
        if (Objects.isNull(finishOrderProductList) || finishOrderProductList.size() == 0){
            return null;
        }
        return finishOrderProductList.get(0);
    }


    @Override
    public Boolean updateServiceTimesByProductNum(String productNum, int remainNum) {
        return finishOrderProductCustomMapper.updateServiceTimesByProductNum(productNum, remainNum);
    }

    /**
     * 新增
     * @param finishOrderProduct
     */
    @Override
    public Integer insertFinishProduct(FinishOrderProduct finishOrderProduct) {
        return finishOrderProductMapper.insertSelective(finishOrderProduct);
    }

    /**
     * 订单完成的产品表
     * @param productOrderId 订单id
     * @param productCode 产品编码
     */
    @Override
    public List<FinishOrderProduct> selectFinshByOrderIdAndCode(String productOrderId, String productCode) {
        FinishOrderProductExample example = new FinishOrderProductExample();
        FinishOrderProductExample.Criteria criteria = example.createCriteria();
        criteria.andProductOrderIdEqualTo(productOrderId);
        criteria.andProductCodeEqualTo(productCode);
        criteria.andDelFlagEqualTo("0");
        List<FinishOrderProduct> list = finishOrderProductMapper.selectByExample(example);
        return list;
    }

    /**
     * 修改完成订单的产品表,为删除状态
     * @param finsh 产品表
     * @return
     */
    @Override
    public Boolean updateFinishOrderProduct(FinishOrderProduct finsh) {
        Boolean result = false;
        int num = finishOrderProductMapper.updateByPrimaryKeySelective(finsh);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 订单管理-订单列表-订单详情-产品详情
     * @Author pym
     * @Date  2021/9/6 14:42
     * @param productOrderId -产品订单表主键
     * @param productOrderDetailType -订单明细类型--1:产品类型;2:活动类型;3:赠品活动类型;4:兑换类型;5:补款升级
     * @return {@link {java.util.List<com.crm.model.entity.finishOrderProduct.FinishOrderProduct>}}
     */
    @Override
    public List<FinishOrderProduct> selectOrderDetailFinishOrderProductList(String productOrderId, String productOrderDetailType) {
        FinishOrderProductExample finishOrderProductExample = new FinishOrderProductExample();
        FinishOrderProductExample.Criteria criteria = finishOrderProductExample.createCriteria();
        finishOrderProductExample.setOrderByClause("create_tm desc");
        criteria.andDelFlagEqualTo("0");
        criteria.andProductOrderIdEqualTo(productOrderId);
        criteria.andOrderDetailTypeEqualTo(productOrderDetailType);
        List<FinishOrderProduct> finishOrderProductList = finishOrderProductMapper.selectByExample(finishOrderProductExample);
        return finishOrderProductList;
    }

    @Override
    public void delFinishByOrderId(String productOrderId) {
        finishOrderProductCustomMapper.delFinishByOrderId(productOrderId);
    }

    /**
     * 通过订单id查询订单完成的产品
     * @param orderId 订单id
     * @return
     */
    @Override
    public List<FinishOrderProduct> finishList(String orderId) {
        FinishOrderProductExample example = new FinishOrderProductExample();
        FinishOrderProductExample.Criteria criteria = example.createCriteria();
        criteria.andProductOrderIdEqualTo(orderId);
        criteria.andOrderDetailTypeEqualTo("1");
        criteria.andDelFlagEqualTo("0");
        List<FinishOrderProduct> list = finishOrderProductMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<FinishOrderProductVO> selectProductSceneList(String order, String customerId) {
        return finishOrderProductCustomMapper.selectProductSceneList(order,customerId);
    }

    @Override
    public Integer selectFinishProductCountByOrderId(String orderId) {
        return finishOrderProductCustomMapper.selectFinishProductCountByOrderId(orderId);
    }

    @Override
    public Boolean updateFinishOrderProductExchangeStatus(String finishOrderProductId,String exchangeStatus) {
        FinishOrderProduct finishOrderProduct = new FinishOrderProduct();
        finishOrderProduct.setFinishOrderProductId(finishOrderProductId);
        finishOrderProduct.setExchangeStatus(exchangeStatus);
        int num = finishOrderProductMapper.updateByPrimaryKeySelective(finishOrderProduct);
        if(num>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<FinishOrderProduct> selectNoExchangeFinishOrderProductListByOrderId(String orderDetailType,String productCategories, String orderId) {
        FinishOrderProductExample finishOrderProductExample = new FinishOrderProductExample();
        FinishOrderProductExample.Criteria criteria = finishOrderProductExample.createCriteria();
        criteria.andDelFlagEqualTo("0");
        criteria.andOrderDetailTypeEqualTo(orderDetailType);
        if (Objects.nonNull(productCategories)){
            criteria.andProductCategoriesEqualTo(productCategories);
        }
        //查询未兑换的产品
        criteria.andExchangeStatusEqualTo("0");
        criteria.andProductOrderIdEqualTo(orderId);
        List<FinishOrderProduct> finishOrderProductList = finishOrderProductMapper.selectByExample(finishOrderProductExample);
        return finishOrderProductList;
    }

    /**
     * 清空订单下finish表回款数据
     * @param productOrderId
     * @param accountId
     */
    @Override
    public void clearFinishReturnMoneyInfoByOrderId(String productOrderId, String accountId) {
        finishOrderProductCustomMapper.clearFinishReturnMoneyInfoByOrderId(productOrderId,accountId);
    }

    /**
     * 根据订单id产品id查询finish表产品信息
     * @param productId
     * @param productOrderId
     * @return
     */
    @Override
    public List<CustFinanceProductOderVO> selectFinishOrderProductByProductId(String productId, String productOrderId) {
        return finishOrderProductCustomMapper.selectFinishOrderProductByProductId(productId,productOrderId);
    }

    /**
     * 更新全部字段
     * @param finish
     */
    @Override
    public void updateFinishOrderProductAll(FinishOrderProduct finish) {
        finishOrderProductMapper.updateByPrimaryKey(finish);
    }

    /**
     * 查询客户下的产品
     * @param customerId   客户Id
     * @return
     */
    @Override
    public List<CustFinanceProductOderVO> selectProductByCustomerId(String customerId) {
        return finishOrderProductCustomMapper.selectProductByCustomerId(customerId);
    }

    /**
     * 查询客户下的产品
     * @return
     */
    @Override
    public DhPageInfo<PersonalPerformanceAnalysis> getPersonalPerformanceAnalysis(Integer pageIndex, Integer pageSize,String dateStr, String accountId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        List<PersonalPerformanceAnalysis> personalPerformanceAnalysis = finishOrderProductCustomMapper.getPersonalPerformanceAnalysis(dateStr, accountId);
        PageInfo<PersonalPerformanceAnalysis> pageInfo = new PageInfo<>(personalPerformanceAnalysis);

        return new DhPageInfo<PersonalPerformanceAnalysis>(pageInfo, personalPerformanceAnalysis);
    }

}
